﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>对象类要素查询</title>
    <script include="jquery" src="./static/libs/include-lib-local.js"></script>
    <script include="mapboxgl" src="./static/libs/include-mapboxgl-local.js"></script>
    <link href="./static/demo/mapboxgl/example/style.css" rel="stylesheet" type="text/css" />
    <style>
        .mapboxgl-ctrl-icon {
            background-position: 50% 50%;
            background-repeat: no-repeat;
            background-size: 60%;
        }

        .mapboxgl-ctrl-icon.mapboxgl-ctrl-add {
            background-image: url('data:image/svg+xml;utf8,<svg t="1594093104250" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4905"><path d="M789.994 771.154L459.266 670.616l295.152-343.032-380.88 318.478L64.28 553.82l896.008-427.218-170.294 644.552z m-341.168 127.42v-177.38l108.868 30.812-108.868 146.568z" p-id="4906"></path></svg>');
        }

        .mapboxgl-ctrl-icon.mapboxgl-ctrl-update {
            background-image: url('data:image/svg+xml;utf8,<svg t="1594090532673" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1150"><path d="M512 269.922592l0 121.038704 161.384597-161.384597L512 68.191078l0 121.038704c-178.330548 0-322.770218 144.43967-322.770218 322.770218 0 63.343677 18.559705 122.047684 50.02944 171.874509l58.905598-58.905598c-17.953907-33.689287-28.242228-72.018244-28.242228-112.969934C269.922592 378.251833 378.251833 269.922592 512 269.922592zM784.740778 340.124468l-58.905598 58.905598c17.953907 33.689287 28.242228 72.018244 28.242228 112.969934 0 133.748167-108.329241 242.077408-242.077408 242.077408L512 633.038704 350.614379 794.423301l161.384597 161.384597L511.998977 834.770218c178.330548 0 322.770218-144.43967 322.770218-322.770218C834.770218 448.656323 816.210513 389.952316 784.740778 340.124468z" p-id="1151"></path></svg>');
        }

        .mapboxgl-ctrl-icon.mapboxgl-ctrl-delete {
            background-image: url('data:image/svg+xml;utf8,<svg t="1594092749945" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4754"><path d="M517.59 21.609c-100.299 0-181.703 79.514-185.167 179.34H98.603c-25.979 0-47.235 21.099-47.235 47.236 0 25.98 21.099 47.237 47.236 47.237h52.117v528.416c0 99.196 67.233 180.285 150.37 180.285h423.55c82.98 0 150.37-80.616 150.37-180.285V295.737h47.236c25.98 0 47.236-21.1 47.236-47.237 0-25.98-21.099-47.236-47.236-47.236H702.441C699.449 101.124 617.888 21.61 517.59 21.61z m-96.677 179.34c3.464-51.172 45.19-90.85 96.834-90.85s93.37 39.835 96.362 90.85H420.913z m-119.98 714.842c-29.444 0-61.88-37.789-61.88-91.953V295.737h547.311V824.31c0 54.007-32.436 91.954-61.88 91.954H300.933v-0.473z m0 0" p-id="4755"></path><path d="M364.387 802.267c21.57 0 39.363-21.571 39.363-48.653V476.022c0-27.082-17.635-48.654-39.363-48.654-21.572 0-39.364 21.572-39.364 48.654v277.592c0 26.924 17.32 48.653 39.364 48.653z m142.496 0c21.571 0 39.363-21.571 39.363-48.653V476.022c0-27.082-17.635-48.654-39.363-48.654-21.571 0-39.364 21.572-39.364 48.654v277.592c0 26.924 17.793 48.653 39.364 48.653z m149.896 0c21.571 0 39.364-21.571 39.364-48.653V476.022c0-27.082-17.635-48.654-39.364-48.654-21.571 0-39.363 21.572-39.363 48.654v277.592c0 26.924 17.162 48.653 39.363 48.653z m0 0" p-id="4756"></path></svg>');
        }
    </style>
    <script type="text/javascript">
        "use strict";
        //定义地图文档图层和地图
        var mapDocLayer, map;
        var { protocol, ip, port } = window.webclient;

        /** 地图初始化
         */
        function init() {
            //地图容器
            map = new mapboxgl.Map({
                container: 'map',
                minZoom: 3,
                zoom: 6,
                center: [112.247175, 30.152892]
            });
            var navigationControl = new mapboxgl.NavigationControl();
            map.addControl(navigationControl, 'top-left');
            mapDocLayer = new mapboxgl.Zondy.Map.MapDocLayer("Hubei3857", {
                //IP地址
                ip: `${ip}`,
                //端口号
                port: `${port}`,
                //只显示一个图层,不平铺显示
                noWrap: true
            });
            mapDocLayer.addToMap(map);

            var html = '<div class="mapboxgl-ctrl mapboxgl-ctrl-group">' +
                '<button class="mapboxgl-ctrl-icon mapboxgl-ctrl-add" type="button" title="对象类查询" onclick="objFeatureQuery()" aria-label="add"></button>' +
                '<button class="mapboxgl-ctrl-icon mapboxgl-ctrl-delete" type="button" title="删除查询结果" onclick="deleteFeatures()" aria-label="delete"></button>' +
                '</div>';
            $(".mapboxgl-ctrl-top-right").append(html);
            //注册鼠标移动事件
            map.on('mousemove', function (e) {
                //经纬度坐标转web墨卡托
                const earthRad = 6378137.0;
                const x = e.lngLat.lng * Math.PI / 180 * earthRad;
                const a = e.lngLat.lat * Math.PI / 180;
                const y = earthRad / 2 * Math.log((1.0 + Math.sin(a)) / (1.0 - Math.sin(a)));
                document.getElementById('mouse-position').innerHTML = "X轴：" + x.toFixed(2) + "，Y轴：" + y.toFixed(2);
            });
        }

        /** 对象类要素查询
         */
        function objFeatureQuery() {
            startPressBar();
            if (map.getLayer("highlayer")) {
                map.getLayer("highlayer").visibility = "visible";
                map.repaint = true;
                stopPressBar();
                return;
            }
            //初始化查询结构对象，设置查询结构包含几何信息
            var queryStruct = new Zondy.Service.QueryFeatureStruct({
                IncludeGeometry: true,//是否包含几何图形信息
                IncludeAttribute: true,//是否包含属性信息
                IncludeWebGraphic: false//是否包含图形显示参数
            });
            //gdbp://MapGisLocal/ClientTheme/ds/epsg4326/sfcls/湖北省市级区划2
            //实例化查询参数对象
            var queryParam = new Zondy.Service.QueryByLayerParameter("gdbp://MapGisLocal/ClientTheme/ds/epsg4326/sfcls/湖北省市级区划2", {
                objectIds: "4,5",
                pageIndex: 0,
                recordNumber: 50,
                struct: queryStruct,
            });
            //实例化地图文档查询服务对象
            var queryService = new Zondy.Service.QueryLayerFeature(queryParam, {
                //IP地址
                ip: `${ip}`,
                //端口号
                port: `${port}`
            });
            //执行查询操作，querySuccess为成功回调，queryError为失败回调
            queryService.query(querySuccess, queryError);
        }

        /** 查询成功回调函数
         *  @param {json对象} result 获取结果对象
         */
        function querySuccess(result) {
            //停止进度条
            stopPressBar();
            //获取查询到的结果数组,ploygonArr的个数即为查询到的结果数
            var ploygonArr = result.SFEleArray;
            var features = [];
            for (var i = 0; i < ploygonArr.length; i++) {
                //获取要素几何数组
                var Rings = ploygonArr[i].fGeom.RegGeom[0].Rings;
                //针对复合要素，要循环获取每一个几何
                for (var j = 0; j < Rings.length; j++) {
                    //取出构成多边形的数组
                    var dots = Rings[j].Arcs[0].Dots;
                    var finaldots = [];
                    for (var k = 0; k < dots.length; k++) {
                        //组织面的坐标数组
                        finaldots.push([dots[k].x, dots[k].y]);
                    }
                    var feature = {
                        "type": "Feature",
                        "geometry": {
                            "type": "Polygon",
                            "coordinates": [finaldots]
                        }
                    }
                    features.push(feature);
                }
            }
            //用geojson创建一个多边形
            var geometryPolygon = {
                "type": "FeatureCollection",
                "features": features
            };
            var source = {
                "type": "geojson",
                "data": geometryPolygon
            };
            map.addLayer({
                //此id可随意设置，但是要唯一
                "id": "highlayer",
                //指定类型为fill（填充区域）
                "type": "fill",
                //设置数据来源
                "source": source,
                //设置绘制参数
                "paint": {
                    //设置填充颜色
                    "fill-color": '#7FFF00',
                    //设置透明度
                    "fill-opacity": 0.5,
                    "fill-outline-color": '#FFA500'
                }
            });
            stopPressBar()
        }

        /** 查询失败回调函数
         */
        function queryError() {
            //停止进度条
            stopPressBar();
            alert("查询失败！");
        }

        /** 删除绘制的要素
         */
        function deleteFeatures() {
            //停止进度条
            stopPressBar();
            //隐藏图层
            if (map.getLayer("highlayer")) {
                map.getLayer("highlayer").visibility = "none";
                map.repaint = true;
            }
        }

        /** 开始进度条动画
         */
        function startPressBar() {
            document.getElementById('preview').style.display = "";
        }

        /** 停止进度条动画
         */
        function stopPressBar() {
            document.getElementById('preview').style.display = "none";
        }
    </script>
</head>

<body onload="init()">
    <div id="preview"
        style="display: none; text-align: center; padding-top: 250px; font-weight: bold;position: absolute;background: rgba(3, 3, 3, 0.1);color: #fff;float: left; width: 100%;height: 100%;-webkit-border-radius: 10px;-moz-border-radius: 10px;border-radius: 10px;z-index: 20000;">
        <img src="./static/assets/graphic-image/39-1.gif" alt='' /><br />
        <br />
        <span>正在操作，请稍候</span>
    </div>
    <div id="map" style="width: 100%; height:700px;">
        <div id="mouse-position">
        </div>
    </div>
</body>

</html>